package com.eoner.managerlibrary.user;

import com.eoner.baselibrary.bean.personal.CustomerOrderInfo;
import com.eoner.commonbean.user.CustomerBean;
import com.eoner.managerlibrary.system.SystemConfigStorage;
import com.eoner.managerlibrary.user.annotation.UpdateAllUserCache;
import com.eoner.managerlibrary.user.annotation.UpdateCacheByApi;
import com.eoner.managerlibrary.user.cache.UserDataCache;
import com.eoner.managerlibrary.user.callback.UserDataCallBack;
import com.eoner.managerlibrary.user.dispatch.UserDataRequest;
import com.eoner.managerlibrary.user.dispatch.UserDataStorage;

public class UserDataManager implements IUserDataManager {

    private static UserDataManager instance;

    //用户信息请求
    private UserDataRequest mUserDataRequest;
    //用户信息存储
    private UserDataStorage mDataStorage;

    public static UserDataManager getInstance() {
        synchronized (UserDataManager.class) {
            if (instance == null) {
                instance = new UserDataManager();
            }
        }
        return instance;
    }

    public UserDataRequest getUserDataRequest() {
        if (mUserDataRequest == null) {
            mUserDataRequest = new UserDataRequest();
        }
        return mUserDataRequest;
    }

    public UserDataStorage getDataStorage() {
        if (mDataStorage == null) {
            mDataStorage = new UserDataStorage();
        }
        return mDataStorage;
    }

    ///临时变量(先这样做)
    public boolean isShowAfterAddress = false;

    /////////////////////////////////////////////////////////////////////////////////
    //
    //                     登录状态操作
    //
    /////////////////////////////////////////////////////////////////////////////////

    @Override
    public boolean isSystemLogin() {
        return getDataStorage().isLogin();
    }

    @Override
    public void autoSystemLogin() {
        getDataStorage().autoLogin();
    }

    /////////////////////////////////////////////////////////////////////////////////
    //
    //                    请求相关
    //
    /////////////////////////////////////////////////////////////////////////////////

    @Override
    public void requestCustomerInfo(final UserDataCallBack callBack) {
        getUserDataRequest().requestCustomerInfo(new UserDataCallBack() {
            @Override
            public void onSuccess() {
                if (callBack != null) {
                    callBack.onSuccess();
                }
            }

            @Override
            public void onSuccess(CustomerBean bean) {
                if (callBack != null) {
                    callBack.onSuccess(bean);
                }
            }

            @Override
            public void progressCustomerData(CustomerBean bean) {
                //处理数据
                UserDataManager.getInstance().updateUserInfo(bean.getData());
            }

            @Override
            public void onFailed(String errMsg) {
                super.onFailed(errMsg);
                if (callBack != null) {
                    callBack.onFailed(errMsg);
                }
            }
        });
    }

    /**
    * 内部刷新customerInfo缓存
    * */
    private void requestCustomerInfoWithMobile() {
        String mobile = getDataStorage().getMobile();
        String checkIdCardStatus = getDataStorage().getCheckIdCardStatus();
        if (mobile != null && mobile.length() > 0 && checkIdCardStatus != null && checkIdCardStatus.length() > 0) {
            return;
        }
        if (!isSystemLogin()) {
            return;
        }
        getUserDataRequest().requestCustomerInfoWithoutMobile(getDataStorage().getCustomerToken(),new UserDataCallBack() {
            @Override
            public void progressCustomerData(CustomerBean bean) {
                //处理数据
                UserDataManager.getInstance().updateUserInfo(bean.getData());
            }
        });
    }

    @Override
    public void requestCustomerInfoForceUpdate(final UserDataCallBack callBack) {
        getUserDataRequest().requestCustomerInfoForceUpdate(new UserDataCallBack() {

            @Override
            public void progressCustomerData(CustomerBean bean) {
                //处理数据
                UserDataManager.getInstance().updateUserInfo(bean.getData());
            }

            @Override
            public void onSuccess() {
                if (callBack != null) {
                    callBack.onSuccess();
                }
            }

            @Override
            public void onSuccess(CustomerBean bean) {
                if (callBack != null) {
                    callBack.onSuccess(bean);
                }
            }

            @Override
            public void onFailed(String errMsg) {
                super.onFailed(errMsg);
                if (callBack != null) {
                    callBack.onFailed(errMsg);
                }
            }
        });
    }

    @Override
    public void requestCustomerOrderIndex(final UserDataCallBack callBack) {

        getUserDataRequest().requestCustomOrderIndex(new UserDataCallBack() {

            @Override
            public void progressCustomOrderData(CustomerOrderInfo orderInfo) {
                //处理数据
                UserDataManager.getInstance().updateUserInfo(orderInfo.getData());
            }

            @Override
            public void onSuccess() {
                if (callBack != null) {
                    callBack.onSuccess();
                }
            }

            @Override
            public void onSuccess(CustomerOrderInfo orderInfo) {
                if (callBack != null) {
                    callBack.onSuccess(orderInfo);
                }
            }

            @Override
            public void onFailed(String errMsg) {
                super.onFailed(errMsg);
                if (callBack != null) {
                    callBack.onFailed(errMsg);
                }
            }
        });
    }

    @Override
    public UserDataCache getUserCacheData() {
        return getDataStorage().getUserInfo();
    }

    /////////////////////////////////////////////////////////////////////////////////
    //
    //                   用户信息获取相关
    //
    /////////////////////////////////////////////////////////////////////////////////

    @Override
    public boolean isUserDisable() {
        return getDataStorage().isUserDisable();
    }

    @Override
    public String getCustomerId() {
        return getDataStorage().getCustomerId();
    }

    @Override
    public String getCustomerToken() {
        return getDataStorage().getCustomerToken();
    }

    @Override
    @UpdateCacheByApi
    public String getHeadImgUrl() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getHeadImgUrl();
    }

    @Override
    @UpdateCacheByApi
    public String getNickName() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getNickName();
    }

    @Override
    @UpdateCacheByApi
    public String getLevel_bg() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getLevel_bg();
    }


    @Override
    @UpdateCacheByApi
    public String getMobile() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getMobile();
    }

    @Override
    @UpdateCacheByApi
    public String getIdCardNo() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getIdCardNo();
    }

    @Override
    @UpdateCacheByApi
    public String getIdCardRealName() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getIdCardRealName();
    }

    //获取身份提示信息
    @Override
    @UpdateCacheByApi
    public String getIdCardMessage() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getIdCardMessage();
    }

    //是否是代理商
    @Override
    @UpdateCacheByApi
    public boolean isAgent() {
        requestCustomerInfoWithMobile();
        return getDataStorage().isAgent();
    }

    @Override
    @UpdateCacheByApi
    public String getRoleLevel() {
        requestCustomerInfoWithMobile();
        return getDataStorage().userRoleLevel();
    }

    @Override
    @UpdateCacheByApi
    public String getInviteCode() {
        requestCustomerInfoWithMobile();
        return getDataStorage().inviteCode();
    }

    @Override
    public String getCheckIdCardStatus() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getCheckIdCardStatus();
    }

    @Override
    public boolean getCheckRefundAddress() {
        requestCustomerInfoWithMobile();
        return getDataStorage().getCheckRefundAddress();
    }

    /////////////////////////////////////////////////////////////////////////////////
    //
    //                    用户信息修改相关
    //
    /////////////////////////////////////////////////////////////////////////////////

    @Override
    @UpdateAllUserCache
    public void updateUserInfo(CustomerBean.DataBean dataBean) {
        getDataStorage().updateUserInfo(dataBean);
    }

    @Override
    @UpdateAllUserCache
    public void updateUserInfo(CustomerOrderInfo.DataBean dataBean) {
        getDataStorage().updateUserInfo(dataBean);
    }

    @Override
    public void updateUserStatus(String isDisable) {
        getDataStorage().updateUserStatus(isDisable);
    }

    @Override
    public void updateUserStatus(boolean isDisable) {
        getDataStorage().updateUserStatus(isDisable);
    }

    @Override
    public void updateUserMobile(String mobile) {
        getDataStorage().updateUserMobile(mobile);
    }

    @Override
    public void updateUserIdCardRealName(String realName) {
        getDataStorage().updateUserIdCardRealName(realName);
    }

    @Override
    public void updateUserIdCardNumber(String idCardNumber) {
        getDataStorage().updateUserIdCardNumber(idCardNumber);
    }

    @Override
    public void updateUserIsAgent(boolean isAgent) {
        getDataStorage().updateUserIsAgent(isAgent);
    }

    @Override
    public void updateUserLevel(String userLevel) {
        getDataStorage().updateUserLevel(userLevel);
    }

    @Override
    public void updateInviteCode(String inviteCode) {
        getDataStorage().updateInviteCode(inviteCode);
    }

    @Override
    public void updateCheckRefundAddress(boolean checkRefundAddress) {
        getDataStorage().updateCheckAddress(checkRefundAddress);
    }



    @Override
    public void clearLoginInfo() {
        getDataStorage().clearLoginInfo();
    }



}
